package com.coolgor.axnut.web.auth;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.axnut.util.HttpServletRequestUtil;
import com.coolgor.axnut.dto.auth.OperationTraceExecution;
import com.coolgor.axnut.entity.auth.LocalUser;
import com.coolgor.axnut.entity.auth.OperationTrace;
import com.coolgor.axnut.entity.auth.Role;
import com.coolgor.axnut.service.auth.OperationTraceService;


@Controller
@RequestMapping(value="trace", method={RequestMethod.GET,RequestMethod.POST})
public class OperationTraceController {
	@Autowired
	private OperationTraceService operationTraceService;
	
	@RequestMapping(value = "/touch", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> touch(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		OperationTrace operationTrace = new OperationTrace();
		//从request中获取recordID
	    Long recordID = HttpServletRequestUtil.getLong(request, "recordID");
	    operationTrace.setRecordID(recordID);
		//从session中获取用户信息
	    LocalUser localUser = (LocalUser) request.getSession().getAttribute("login_user");
	    operationTrace.setUserID(localUser.getUserID());
	    operationTrace.setUsername(localUser.getUsername());
	    operationTrace.setNameCN(localUser.getNameCN());
	    //从session中获取当前角色信息
	    Role loginRole = (Role) request.getSession().getAttribute("login_role");
	    operationTrace.setRoleID(loginRole.getRoleID());
	    operationTrace.setRoleName(loginRole.getRoleName());
	    //从request中获取 当前操作的IP地址    
	    operationTrace.setSource(HttpServletRequestUtil.getIP(request));
		//从request中获取 目标路径的字符串	    
	    String target = HttpServletRequestUtil.getString(request, "target");
	    if (!"".equals(target)||null!=target) {
	    	operationTrace.setTarget(target);
	    }
		//从request中获取 当前操作的字符串	    
	    String operation = HttpServletRequestUtil.getString(request, "operation");
	    if (!"".equals(operation)||null!=operation) {
	    	operationTrace.setOperation(operation);
	    }
	    OperationTraceExecution exe = operationTraceService.touch(operationTrace);
		if (null != exe ) {
			try {
				modelMap.put("record_id",exe.getOperationTrace().getRecordID());
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Touch Error!");
		}
		return modelMap;
	}	
	
	@RequestMapping(value = "/getOperationTraceList", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getOperationTraceList(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    int page = HttpServletRequestUtil.getInt(request, "page");
	    int pageSize = HttpServletRequestUtil.getInt(request, "rows");
	    OperationTrace operationTraceCondition = new OperationTrace();
	    String usernameCondition = HttpServletRequestUtil.getString(request, "username");
	    if (!"".equals(usernameCondition)||null!=usernameCondition) {
	    	operationTraceCondition.setUsername(usernameCondition);
	    }
	    String usernameCNCondition = HttpServletRequestUtil.getString(request, "usernameCN");	    
	    if (!"".equals(usernameCNCondition)||null!=usernameCNCondition) {
	    	operationTraceCondition.setNameCN(usernameCNCondition);
	    }
	    OperationTraceExecution exe = operationTraceService.getOperationTraceList(operationTraceCondition, page, pageSize);
		if (null != exe ) {
			try {
				modelMap.put("total",exe.getCount());
				modelMap.put("rows",exe.getOperationTraceList());
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty List");
		}
		return modelMap;
	}	
}
